package com.probiz.estore.product.service.impl;

import java.util.List;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductReview;
import com.probiz.estore.common.model.product.ProductStat;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.ProductReviewManager;
import com.probiz.estore.product.service.ProductReviewNtManager;
import com.probiz.estore.product.service.ProductStatManager;

public class ProductReviewNtManagerImpl implements ProductReviewNtManager{
	
	private ProductReviewManager productReviewManager = null;
	
	private ProductStatManager productStatManager = null;

	private ProductManager productManager = null;
	

	public void setProductReviewManager(ProductReviewManager productReviewManager) {
		this.productReviewManager = productReviewManager;
	}

	public void setProductStatManager(ProductStatManager productStatManager) {
		this.productStatManager = productStatManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductReviewNtManager#calReplyCountForScheduling()
	 */
	public void calReplyCountForScheduling() {
		String hql = "from ProductReview pr where pr.parent is null";//获得评论
		boolean flag =true;
		int pageSize = 10;
		int pageNo = 1;
		do{
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql,null,pageNo,pageSize,null);
			List<ProductReview> productReviewList = productManager.searchByCriteria(sc);
			for(ProductReview productReview: productReviewList){
				try{
					productReview.setReplyCount(productReviewManager.getReplyCountByProductReviewId(productReview.getProductReviewId()).intValue());
					productReview.setOnShowReplyCount(productReviewManager.getOnShowReplyCountByProductReviewId(productReview.getProductReviewId()).intValue());
					productReviewManager.save(productReview);
					productReviewManager.flush();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(productReviewList.size()!=pageSize){
				flag=false;
			}else{
				++pageNo;
			}
		}while(flag);	 
    }
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductReviewNtManager#calReviewCountAndScoreForScheduling()
	 */
	public void calReviewCountAndScoreForScheduling(){
		String hql = "from Product where status != -1 ";//获得评论
		boolean flag =true;
		int pageSize = 10;
		int pageNo = 1;
		do{
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql,null,pageNo,pageSize,null);
			List<Product> productList = productManager.searchByCriteria(sc);
			for(Product product: productList){
				try{
					ProductStat stat = productStatManager.getById(product.getProductStatId());
					stat.setReviewCount(productReviewManager.getReviewCountByProductId(product.getProductId()).intValue());
					stat.setOnShowReviewCount(productReviewManager.getOnShowReviewCountByProductId(product.getProductId()).intValue());
					stat.setReviewScore(productReviewManager.getReviewScoreByProductId(product.getProductId()));
					productStatManager.save(stat);
					productStatManager.flush();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(productList.size()!=pageSize){
				flag=false;
			}else{
				++pageNo;
			}
		}while(flag);	 
	}
}
